//
//  BaseViewController.swift
//  CXWeibo
//
//  Created by 陈旭 on 16/9/11.
//  Copyright © 2016年 陈旭. All rights reserved.
//

import UIKit

class BaseViewController: UITableViewController {
    
    // MARK:- 懒加载属性
    lazy var visitorView : VisitorView = VisitorView.visitorView()
    // MARK:- 定义变量
    var isLogin : Bool = false
    
    // MARK:- 系统回调函数
    override func loadView() {
        var accountPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first!
        accountPath = (accountPath as NSString).stringByAppendingPathComponent("account.plist")
        let account = NSKeyedUnarchiver.unarchiveObjectWithFile(accountPath) as? UserAccount
        if let account = account{
            if let expires_date = account.expires_date {
                isLogin = expires_date.compare(NSDate()) == NSComparisonResult.OrderedDescending// 判断token是否过期
            }
        }
        isLogin ? super.loadView() : setupVisitorView()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupNavigationItems()
        
        visitorView.registerBtn.addTarget(self, action: "registerBtnClick", forControlEvents: .TouchUpInside)
        visitorView.loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: .TouchUpInside)
    }
}

// MARK:- 设置UI界面
extension BaseViewController{
    private func setupVisitorView(){
        view = visitorView
    }
    
    private func setupNavigationItems(){
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .Plain, target: self, action: "registerBtnClick")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登陆", style: .Plain, target: self, action: "loginBtnClick")
    }
}
// MARK:- 事件监听
extension BaseViewController{
    @objc private func registerBtnClick(){
        CXPrint("registerBtnClick")
    }
    @objc private func loginBtnClick(){
        let oauthVC = OAuthViewController()
        let oauthNav = UINavigationController(rootViewController: oauthVC)
        presentViewController(oauthNav, animated: true, completion: nil)
    }
}

